package com.hkfg.controller;

import com.hkfg.domain.Friend;
import com.hkfg.service.impl.FriendServiceImpl;
import com.hkfg.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller   // bean
@ResponseBody
@CrossOrigin
public class FriendController {

    @Autowired
    private FriendServiceImpl friendService;
    @Autowired
    private UserServiceImpl userService;

    /**
     * 返回所有好友
     * @return list
     */
    @RequestMapping(value = "/friendlist", method = RequestMethod.GET)
    public List<Friend> friendList(@RequestParam("token") String id){
        System.out.println(id);
        return friendService.selectAllFriends(Integer.parseInt(id));
    }

    /**
     * 根据好友id删除好友
     * @param id
     * @return 返回1即删除成功，返回0即删除失败，可以和前端确认更想要字符串还是int
     */
    @RequestMapping(value = "/deletefriend")
    public List<Friend> deleteFriend( String id, String fname){   // fname是lyt使用的名称，需要和前端确认一下他们的名字，就是传给我好友的名字
        if(friendService.deleteFriend(Integer.parseInt(id), fname) > 0)
            return friendList(id);
        else
            return null;
    }

    /**
     * 传入用户id和好友id进行添加好友的操作
     * @param userId
     * @param fname
     * @return
     */
    @RequestMapping(value = "/addfriend")
    public List<Friend> addFriend(@RequestParam("token")String userId, String fname){
        int id = userService.getUserId(fname);
        if(friendService.addFriend(Integer.parseInt(userId), id) > 0){
            return friendList(userId);
        }else {
            return null;
        }
    }

}
